//
//  BLDouyuDanmuCell.swift
//  BLLiveTelecast
//
//  Created by BigL055 on 16/6/17.
//  Copyright © 2016年 bigL.liveTelecast.com. All rights reserved.
//

import UIKit
import YYText

class BLDouyuDanmuCell: UITableViewCell {
    
    
    let contentLabel = YYLabel()
    
    var modal : BLDanmuModel?{
        didSet{
            
            let typeStr = modal?.type
            
                /// 聊天弹幕
            if typeStr == BLCellType.CellChatMessage {
                setChatMessageLabel(modal!)
            }
                /// 赠送礼物消息
            else if typeStr == BLCellType.CellGiftGivingNotice {
                setGiftGivingNoticeLabel(modal!)
            }
                /// 用户进房通知消息
            else if typeStr == BLCellType.CellUserEnter {
                setUserEnterLabel(modal!)
            }
                /// 管理员封禁用户发言
            else if typeStr == BLCellType.CellBanUser {
                
            }
                /// 用户升级
            else if typeStr == BLCellType.CellUpGrade {
                setUpGradeLabel(modal!)
            }
            
            
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //取消选中
        self.selectionStyle = .None
        
        contentLabel.sizeToFit()
        contentLabel.font = bl_Font_14
        
        contentLabel.fadeOnAsynchronouslyDisplay = true
        contentView.addSubview(contentLabel)
        contentLabel.snp_makeConstraints { (make) in
            make.right.bottom.equalToSuperview().offset(0)
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
}

// MARK: - 事件响应
extension BLDouyuDanmuCell{
    
    func nickBtnClick(butto : UIButton) {

        let blackView = BLBlacklistView.viewNew()
        
        
blackView.setBlacklistViewInfo("", nickNameStr: (modal?.nickName)!, level: (modal?.level)!)
        blackView.bl_setLayerShadow(UIColor.grayColor(), offset: CGSize(width: 1,height: 1), radius: 10)
        blackView.show()
        
        
    }
}


private extension BLDouyuDanmuCell{
    
    
    /// 聊天弹幕Label
    func setChatMessageLabel(modal : BLDanmuModel) {
        // 创建混排字符串
        let contentAttStr = NSMutableAttributedString()
        
        // 等级图标
        contentAttStr.appendAttributedString(getLevelImageViewAttStr(modal.level))
        contentAttStr.yy_appendString(" ")
        // 用户名
        contentAttStr.appendAttributedString(getNickNameBtnAttStr(modal.nickName!,BtnColor: UIColor.blueColor()))
        contentAttStr.yy_appendString(" : ")
        // 正文
        contentAttStr.yy_appendString((modal.text)!)
        
        //设置解析器
        contentLabel.textParser = modal.emoteParser
        //设置富文本
        contentLabel.attributedText = contentAttStr
    }
    
    /// 用户进房通知消息Label
    func setUserEnterLabel(modal : BLDanmuModel) {
        // 创建混排字符串
        let contentAttStr = NSMutableAttributedString()
        
        // douyu图标
        contentAttStr.appendAttributedString(getDouyuImageViewAttStr())
        contentAttStr.yy_appendString(" 欢迎 ")
        
        // 等级图标
        contentAttStr.appendAttributedString(getLevelImageViewAttStr(modal.level))
        contentAttStr.yy_appendString(" : ")
        
        // 用户名
        contentAttStr.appendAttributedString(getNickNameBtnAttStr(modal.nickName!))
        contentAttStr.yy_appendString(" 来到本直播间!")
        
        //设置解析器
        contentLabel.textParser = modal.emoteParser
        //设置富文本
        contentLabel.attributedText = contentAttStr
    }
    
    
    /// 用户送礼物通知消息Label
    func setGiftGivingNoticeLabel(modal : BLDanmuModel) {
        
        // 创建混排字符串
        let contentAttStr = NSMutableAttributedString()
        
        // 等级图标
        contentAttStr.appendAttributedString(getLevelImageViewAttStr(modal.level))
        contentAttStr.yy_appendString(" ")
        // 用户名
        contentAttStr.appendAttributedString(getNickNameBtnAttStr(modal.nickName!))
        contentAttStr.yy_appendString(" 赠送给主播")
        
        //礼物ID
        let yuwanStr = "100鱼丸 "
        let yuwanText = NSMutableAttributedString(string: yuwanStr)
        yuwanText.addAttribute(NSForegroundColorAttributeName, value: UIColor.orangeColor(), range: NSMakeRange(0, yuwanStr.characters.count))
        
        contentAttStr.appendAttributedString(yuwanText)
        contentAttStr.yy_appendString(modal.giftID!)
        contentAttStr.yy_appendString(" ")
        
        // 正文
        contentAttStr.yy_appendString((modal.hitCount)!)
        contentAttStr.yy_appendString("连击")
        
        //设置解析器
        contentLabel.textParser = modal.emoteParser
        //设置富文本
        contentLabel.attributedText = contentAttStr
    }
    
    /// 用户升级Label
    func setUpGradeLabel(modal : BLDanmuModel) {
        
        // 创建混排字符串
        let contentAttStr = NSMutableAttributedString()
        
        // 用户名
        contentAttStr.appendAttributedString(getNickNameBtnAttStr(modal.nickName!))
        contentAttStr.yy_appendString(" 升级到 ")
        
        // 等级图标
        contentAttStr.appendAttributedString(getLevelImageViewAttStr(modal.level))
        contentAttStr.yy_appendString(" ")
        
        //设置解析器
        contentLabel.textParser = modal.emoteParser
        //设置富文本
        contentLabel.attributedText = contentAttStr
        
    }
    
    /// 用户被房管禁言Label
    func setBanUserLabel(modal : BLDanmuModel) {
        
        // 创建混排字符串
        let contentAttStr = NSMutableAttributedString()
        //禁言通知
        let banStr = modal.nickName! + "被房管禁言!"
        contentAttStr.yy_appendString(banStr)
        //设置富文本
        contentLabel.attributedText = contentAttStr
        
    }

    
    
}


// MARK: - 字符组件
private extension BLDouyuDanmuCell{
    
    // 用户名按钮 字符组件 默认橙色
    func getNickNameBtnAttStr(nickName : String,BtnColor : UIColor = UIColor.orangeColor()) -> NSMutableAttributedString {
        let nickNameBtn = UIButton(type: .Custom)
        // 用户名
        nickNameBtn.setTitle(nickName, forState: UIControlState.Normal)
        nickNameBtn.setTitleColor(BtnColor, forState: UIControlState.Normal)
        nickNameBtn.titleLabel?.font = bl_Font_10
        nickNameBtn.sizeToFit()
        nickNameBtn.addTarget(self, action: #selector(BLDouyuDanmuCell.nickBtnClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        let nickNameBtnAttStr = NSMutableAttributedString.yy_attachmentStringWithContent(nickNameBtn, contentMode: UIViewContentMode.Center,attachmentSize : nickNameBtn.frame.size, alignToFont: bl_Font_10, alignment: YYTextVerticalAlignment.Center)
        return nickNameBtnAttStr
    }
    
    /// 等级图标 字符组件
    func getLevelImageViewAttStr(level : String) -> NSMutableAttributedString {
        
        let levelImageView = UIImageView(frame: CGRectMake(0, 0, 21, 10))
        // 等级图标
        let levelImage = UIImage(named: "Level_\(level)")
        levelImageView.image = levelImage
        let levelImageViewAttStr = NSMutableAttributedString.yy_attachmentStringWithContent(levelImageView, contentMode: UIViewContentMode.Center,attachmentSize : levelImageView.frame.size, alignToFont: bl_Font_10, alignment: YYTextVerticalAlignment.Center)
        return levelImageViewAttStr
    }
    
    /// 斗鱼图标  字符组件
    func getDouyuImageViewAttStr() -> NSMutableAttributedString {
        
        let douyuImageView = UIImageView(frame: CGRectMake(0, 0, 21, 21))
        // 斗鱼图标
        let douyuImage = UIImage(named: "Image_silver_chat_36x26_")
        douyuImageView.image = douyuImage
        let douyuImageViewAttStr = NSMutableAttributedString.yy_attachmentStringWithContent(douyuImageView, contentMode: UIViewContentMode.Center,attachmentSize : douyuImageView.frame.size, alignToFont: bl_Font_10, alignment: YYTextVerticalAlignment.Center)
        return douyuImageViewAttStr
        
    }
    
}

